package com.bii.gelapp;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

import org.opencv.core.Scalar;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class MarkerValueActivity extends Activity {
	boolean editmode = false;
	public Bitmap originalBmp; // origin
	public static ArrayList<ConnectedComponent> markerlist = new ArrayList<ConnectedComponent>();
	public static ArrayList<int[]>colorlist = new ArrayList<int[]>();
	//final arraylist
	public static ArrayList<MarkerConnectedComponent> markersconnectedlist = new ArrayList<MarkerConnectedComponent>();
	public Menu menu;
	
	public static String markerstandardtitle;

	public Bitmap getOriginalBmp() {
		return originalBmp;
	}

	public void setOriginalBmp(Bitmap bmp) {
		originalBmp = bmp;
	}

	public static ArrayList<MarkerConnectedComponent> getmarkersconnectedlist() {
		return markersconnectedlist;
	}
	
	public MarkervalueTouchImageView imgView;
	
	//public TouchImageView imgView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.markervalueactivity);
		if (getIntent().hasExtra("markerlist")) {
			markerlist.clear();
			markerlist = (ArrayList<ConnectedComponent>) getIntent()
					.getSerializableExtra("markerlist");
		}
		
		if(getIntent().hasExtra("colorlist")){
			colorlist.clear();
			colorlist = (ArrayList<int[]>) getIntent()
			.getSerializableExtra("colorlist");

		}
		markersconnectedlist.clear();
		for(int i = 0; i < markerlist.size(); i++)
		{
			ConnectedComponent cc = markerlist.get(i);
			int[] color = colorlist.get(i);
			MarkerConnectedComponent mcc = new MarkerConnectedComponent();
			mcc.top = cc.top;
			mcc.bottom = cc.bottom;
			mcc.left = cc.left;
			mcc.right = cc.right;
			mcc.value = 0;
			mcc.color = color;
			markersconnectedlist.add(mcc);
		}
		
		byte[] byteArray = getIntent().getByteArrayExtra("BMP");
		originalBmp = BitmapFactory.decodeByteArray(byteArray, 0,
				byteArray.length);
		
		imgView = new MarkervalueTouchImageView(this, originalBmp);

		imgView.setMaxZoom(4f);

		setContentView(imgView);
		imgView.setEditMode(true);
		
		// instructions
				String donotshow = "";
				final Dialog instructiondialog = new Dialog(MarkerValueActivity.this);
				instructiondialog.setContentView(R.layout.instructiondialog);
				instructiondialog.setTitle("Attention");
				instructiondialog.setCancelable(true);
				instructiondialog.setCancelable(false);

				TextView tvInstructions = (TextView) instructiondialog
						.findViewById(R.id.tvInstructions);
				ImageView imgView1 = (ImageView) instructiondialog
						.findViewById(R.id.ivInstructions);
				final CheckBox donotshowbox = (CheckBox) instructiondialog.findViewById(R.id.donotshow);
				Button btOk = (Button) instructiondialog.findViewById(R.id.btOk);

				final SharedPreferences donotshowinstructions = this.getSharedPreferences(
						"instructions", 0);
				donotshow = donotshowinstructions.getString("markermarkervalueeditinstruction",
						"nothing");
				if (donotshow.equals("nothing")) {
					donotshow = "Show";
				}

				if (donotshow.equals("Show")) {
					tvInstructions.setText("Enter the size of markers manually by clicking on the red rectangles"
							);
					
					Drawable drawable = getResources().getDrawable(R.drawable.markermarkervalueedit_instruction);

					Bitmap instructionbmp = ((BitmapDrawable)drawable).getBitmap();
					
					imgView1.setImageBitmap(instructionbmp);
					
					instructiondialog.show();

					btOk.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							if(donotshowbox.isChecked()){
								SharedPreferences.Editor editor = donotshowinstructions.edit();
								editor.putString("markermarkervalueeditinstruction", "DoNotShow");
								editor.commit();
							}
							else
							{
								SharedPreferences.Editor editor = donotshowinstructions.edit();
								editor.putString("markermarkervalueeditinstruction", "Show");
								editor.commit();
							}
							instructiondialog.dismiss();
						}
					});
					
				}
				//
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu1) {
		// TODO Auto-generated method stub
		this.menu = menu1;
		getMenuInflater().inflate(R.menu.valuemenu, menu1);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case R.id.btConfirm:
			
			SaveDialog sd = new SaveDialog(MarkerValueActivity.this);
			sd.show();
			
			
			break;
		case R.id.btCancel:
			Intent returnActivity2 = new Intent(MarkerValueActivity.this,
					MarkerFullscreenActivity.class);
			
			byte[] bytesorg;
			bytesorg = compressImage(originalBmp);
			returnActivity2.putExtra("BMP", bytesorg);

			if (markerlist.size() != 0) {
				returnActivity2.putExtra("markerlist", markerlist);
			}

			startActivity(returnActivity2);
			break;

			
		}
		return true;
		}
	
	public byte[] compressImage(Bitmap image) {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		image.compress(Bitmap.CompressFormat.PNG, 0, stream);
		byte[] bytes = stream.toByteArray();
		return bytes;
	}
		
		
		
		
	}
	

	
	

